/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package simhya.matheval;

import java.util.ArrayList;

/**
 *
 * @author Luca
 */
public abstract class ExpressionNode {
    ExpressionNode[] children;
    double value;
    int numberOfChildren;
    private int childrenAdded;
    int evaluationCode;
    boolean random;

    
    public ExpressionNode(int numberOfChildren) {
        children = new ExpressionNode[numberOfChildren];
        value = 0.0;
        this.numberOfChildren = numberOfChildren;
        childrenAdded = 0;
        evaluationCode = 0;
        random = false;
    }
    /**
     * Adds a children node
     * @param node
     */
    public void addChild(ExpressionNode node) {
        if (childrenAdded >= numberOfChildren)
            throw new EvalException("Cannot add another children.");
        children[childrenAdded++] = node;
        random = random || node.random;
    }
    /**
     * Adds a child node in the specified position
     * @param node
     * @param position
     */
    public void addChild(ExpressionNode node, int position) {
        if (position >= numberOfChildren)
            throw new EvalException("Child position " + position + " out of bounds");
        children[position] = node;
    }

    public boolean isLogicalExpression() {
        return false;
    }

    public String getExpressionString() {
        return getExpressionString(false);
    }


    public void getListOfVariables(ArrayList<Integer> list) {
        for (int i=0;i<this.numberOfChildren;i++)
            children[i].getListOfVariables(list);
    }

    public boolean isNumericConstant() {
        return false;
    }

    abstract String getExpressionString(boolean fullNumberRepresentation);
    abstract void computeValue();
    abstract void computeValue(SymbolArray varReference);
    abstract void computeValue(int evaluationCode);
    abstract void computeValue(SymbolArray varReference, int evaluationCode);

}
